package com.example.springredisdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/string")
    public String testString() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForValue().set("key1", "111");
        redisTemplate.opsForValue().set("key2", "222");
        redisTemplate.opsForValue().set("key3", "333");

        String value1 = redisTemplate.opsForValue().get("key2");
        String value2 = redisTemplate.opsForValue().get("key100");

        System.out.println("--------------------------------");
        System.out.println("value1: " + value1);
        System.out.println("value2: " + value2);

        return "OK";
    }

    @RequestMapping("/hash")
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key1", "f1", "v1");
        redisTemplate.opsForHash().put("key1", "f2", "v2");
        redisTemplate.opsForHash().put("key1", "f3", "v3");

        String value1 = (String) redisTemplate.opsForHash().get("key1", "f1");
        String value2 = (String) redisTemplate.opsForHash().get("key1", "f2");

        System.out.println("--------------------------------");
        System.out.println("value1: " + value1);
        System.out.println("value2: " + value2);

        redisTemplate.opsForHash().delete("key1",  "f1", "f2");

        Long size = redisTemplate.opsForHash().size("key1");
        System.out.println("size: " + size);
        return "OK";
    }

    @RequestMapping("/list")
    public String testList() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForList().leftPush("key1", "111");
        redisTemplate.opsForList().leftPush("key1", "222");
        redisTemplate.opsForList().leftPush("key1", "333");

        String value1 = redisTemplate.opsForList().rightPop("key1");
        String value2 = redisTemplate.opsForList().rightPop("key1");
        String value3 = redisTemplate.opsForList().rightPop("key1");

        System.out.println("--------------------------------");
        System.out.println("value1: " + value1);
        System.out.println("value2: " + value2);
        System.out.println("value3: " + value3);

        return "OK";
    }

    @RequestMapping("/set")
    public String testSet() {
        System.out.println("--------------------------------");
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key1", "111", "222", "333");
        Set<String> result = redisTemplate.opsForSet().members("key1");
        System.out.println("result: " + result);

        Boolean exists = redisTemplate.opsForSet().isMember("key1", "111");
        System.out.println("exists: " + exists);

        Long count = redisTemplate.opsForSet().size("key1");
        System.out.println("count: " + count);

        redisTemplate.opsForSet().remove("key", "111", "222");
        result = redisTemplate.opsForSet().members("key1");
        System.out.println("result: " + result);
        return "OK";
    }

    @RequestMapping("/zset")
    public String testZset() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key1", "f1", 100);
        redisTemplate.opsForZSet().add("key1", "f2", 200);
        redisTemplate.opsForZSet().add("key1", "f3", 300);

        Set<String> members = redisTemplate.opsForZSet().range("key1", 0 ,-1);
        Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeByScoreWithScores("key", 0 , -1);
        Double score = redisTemplate.opsForZSet().score("key1", "f1");
        Long size = redisTemplate.opsForZSet().remove("key1", "f1");
        Long rank = redisTemplate.opsForZSet().rank("key1", "f2");

        System.out.println("--------------------------------");
        System.out.println("members: " + members);
        System.out.println("membersWithScores: " + membersWithScore);
        System.out.println("score: " + score);
        System.out.println("size: " + size);
        System.out.println("rank: " + rank);

        return "OK";
    }

}
